home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1278 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.3 KB  |  132 lines

  1. Path: news.compuserve.com!newsmaster
  2. From: <75151.03563@compuserve.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: How to ... declare a virtual static function in a class ?
  5. Date: 10 Jan 1996 08:10:44 GMT
  6. Organization: CompuServe Incorporated
  7. Message-ID: <4cvsa4$7g9@dub-news-svc-3.compuserve.com>
  8. NNTP-Posting-Host: dd74-136.compuserve.com
  9. Content-Type: text/plain
  10. Content-length: 3036
  11. X-Newsreader: AIR Mosaic (16-bit) version 1.00.198.07
  12.  
  13.  
  14.  Jean-Pierre Schnyder <jschnyde@worldcom.ch> writes:
  15. >Hi,
  16. >
  17. >I guess this is not possible. Any idea on the rationale about this 
  18. >limitation ? Thanks, Jean-Pierre
  19. >-- 
  20.  
  21. Static virtual functions are not possible because of the way the virtual
  22. function mechanism works in C++, and the nature of static member
  23. functions.
  24.  
  25. Non-static member functions all have an implied parameter passed
  26. to them i.e. <this>.   The this pointer gives the member function access
  27. to the data of a specific instance of an object of the class.
  28.  
  29. Static member functions do not the implied <this> pointer passed to them.
  30. They are, in effect, global functions with funny names.  The static
  31. function member has no way of accessing the non-static data of the
  32. class, unless you explicitly give it access to an object.
  33.  
  34. so given the class
  35.  
  36. class A
  37. {
  38. public:
  39.   int  nonStaticFunc();
  40.   static int staticFunc();
  41.  
  42. private:
  43.   int  m_a;
  44. };
  45.  
  46. int A::nonStaticFunc()
  47. {
  48. return m_a;
  49. }
  50.  
  51. is valid, what the really amounts to is
  52.  
  53. int A::nonStaticFunc( <this> )
  54. {
  55.   return <this>->m_a;
  56. }
  57.  
  58. where <this> is the implied pointer.
  59.  
  60. int A::staticFunc()
  61. {
  62. return m_a;
  63. }
  64.  
  65. is not valid.  The static function gets no <this> pointer, so the compiler
  66. has no way of knowing in which instance of class A to look for the 
  67. variable m_a.
  68.  
  69. All this leads, slowly and painfully up to why static virtual functions are
  70. not possible.
  71.  
  72. In a class that has virtual member functions, the compiler stores a
  73. table of function pointers in each instance of the class.  When the
  74. object is intialized, the compiler puts a pointer to the appropriate function
  75. in the virtual table.  This is how polymorphism works.  When a virtual 
  76. function is called, the compiler looks at the appropriate slot in the virtual 
  77. table for the function to call.
  78.  
  79. class A
  80. {
  81. ...
  82.   virtual int virtFunc();
  83.   static int statFunc()    { return 1; }
  84. ...
  85. };
  86.  
  87. int A::virtFunc()
  88. {
  89. return 1;
  90. }
  91.  
  92. class B
  93. {
  94. ...
  95.   virtual int virtFunc();
  96.   static int statFunc()  { return 10; }
  97. ...
  98. };
  99.  
  100. int B::virtFunc()
  101. {
  102. return 2;
  103. }
  104.  
  105. void func()
  106. {
  107.   A * pa= new A;    // virtual table points to A::virtFunc()
  108.   B * pb= new B;    // virtual table points to B::virtFunc()
  109.   A * pab= new A;  // virtual table points to B::virtFunc()
  110.  
  111.   pa->virtFunc();    // calls A::virtFunc()
  112.   pb->virtFunc();    // calls B::virtFunc()
  113.   pba->virtFunc();  // calls B::virtFunc()
  114.  
  115.   A::statFunc();     // calls A::statFunc()
  116.   B::statFunc():     // calls B::statFunc()
  117.  
  118.   //  pa->statFunc()  - is illegal
  119.   // pb->statFunc()   - is illegal
  120. }
  121.  
  122. Which leads to why virtual static functions don't work.  A virtual 
  123. function must be called from an object with an initialized virtual
  124. function table.  A static member is never associated with an object
  125. instance, but is rather associated with a class.  Since it is not
  126. associated with an object, there is never a virtual table to put
  127. a pointer to the function into.
  128.  
  129. Hope this helps, 
  130. Tom Keane
  131. 75151,03563@compuserve.com
  132.